package com.jcfk.eam.controller.equ;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.domain.dto.equ.EquSparePartsTypeDTO;
import com.jcfk.eam.domain.query.equ.EquSparePartsTypeQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.equ.EquSparePartsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 备件种类 服务接口
 *
 * @author baixuanning
 * @date 2024-08-28
 */
@Api(tags = {"备件类型"})
@RestController
@RequestMapping("parePartsType")
public class EquSparePartsTypeController {

    @Autowired
    private EquSparePartsTypeService sparePartsTypeService;


    /**
     * 查询备件类型列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EquSparePartsTypeQuery", name = "query", value = "", required = true)})
    @ApiOperation(value = "查询备件类型列表", notes = "查询备件类型列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EquSparePartsTypeDTO>> getList(@RequestBody EquSparePartsTypeQuery query) {
        query.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        List<EquSparePartsTypeDTO> list = sparePartsTypeService.getList(query);
        return RetResult.ok(list);
    }


    /**
     * 获取备件类型树
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EquSparePartsTypeQuery", name = "ids", value = "", required = true)})
    @ApiOperation(value = "获取备件类型树", notes = "获取备件类型树", httpMethod = "POST")
    @PostMapping("getTree")
    public RetResult<List<EquSparePartsTypeDTO>> getTree(@RequestBody EquSparePartsTypeQuery query) {
        query.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        return RetResult.ok(sparePartsTypeService.getTree(query));
    }


    /**
     * 获取可用备件类型树
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "EquSparePartsTypeQuery", name = "ids", value = "", required = true)})
    @ApiOperation(value = "获取可用备件类型树", notes = "获取可用备件类型树", httpMethod = "POST")
    @PostMapping("getAvailableTree")
    public RetResult<List<EquSparePartsTypeDTO>> getAvailableTree(@RequestBody EquSparePartsTypeQuery query) {
        query.setEnabled(AvailableStatus.ENABLE.getValue());
        query.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        return RetResult.ok(sparePartsTypeService.getTree(query));
    }

    /**
     * 获取备件种类
     * 根据id 查询详情
     */
    @ApiOperation("获取备件类型")
    @GetMapping("/get/{id}")
    public RetResult<EquSparePartsTypeDTO> get(@PathVariable("id") String id) {
        EquSparePartsTypeDTO item = sparePartsTypeService.get(id);
        return RetResult.ok(item);
    }


    @ApiOperation("保存备件类型")
    @PostMapping("/save")
    @RequestLogHeader(modular = "备件类型", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EquSparePartsTypeDTO eamAssetType) {
        eamAssetType.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        // 数据校验
        RetResult ret = sparePartsTypeService.isValid(eamAssetType);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        // 数据校验
        if (StrUtil.isEmpty(eamAssetType.getId())) {
            // 新增
            sparePartsTypeService.insert(eamAssetType);
        } else {
            // 更新
            sparePartsTypeService.update(eamAssetType);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除备件类型
     */
    @ApiOperation("批量删除备件类型")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "备件类型\"", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EquSparePartsTypeDTO dto) {
        // 校验
        for (String id : dto.getIds()) {
            Boolean haveChild = sparePartsTypeService.isHaveChild(id);
            if (haveChild.equals(true)) {
                return RetResult.fail("不能删除有下级的类型");
            }
        }
        return RetResult.ok(sparePartsTypeService.logicDeleteList(dto));
    }
}
